home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue77 / misc / tips / mail-predictor.py.txt < prev   
Encoding:
Text File  |  2002-08-14  |  2.1 KB  |  74 lines

  1. #! /usr/bin/env python
  2.  
  3. # Here's an example of how you can use the Python rfc822 and
  4. # mailbox modules to make extracting data from mail _easy_.
  5.  
  6. # How likely are you to get an immediate answer back from that email
  7. # you send now?  Find out by checking how active your recipient has
  8. # been over the history of your correspondence.
  9.  
  10. # First argument: recipient's email address.
  11. # Remaining arguments: mailbox files you want to check.
  12.  
  13. import sys, time
  14. import mailbox, rfc822
  15.  
  16. HOUR = 60 * 60.0
  17. WEEK = HOUR * 24 * 7.0
  18.  
  19. total = 0
  20. hit = 0
  21.  
  22. victim = sys.argv[1]
  23.  
  24. # "now" is in seconds since the beginning of the week; displace into
  25. # negative if necessary to make comparisons work.
  26. now = time.time() % WEEK
  27. if now + HOUR > WEEK:
  28.     now = now - WEEK
  29.  
  30. for filename in sys.argv[2:]:
  31.     try:
  32.         box = mailbox.UnixMailbox(open(filename))
  33.     except IOError:
  34.         print "Error opening %s." % filename
  35.         sys.exit()
  36.  
  37.     while (1):
  38.         # keep reading messages from the mailbox as long as
  39.         # any remain.
  40.         message = box.next()
  41.         if not message:
  42.             break
  43.  
  44.         # Here's the fun part. "message" is now an rfc822 object
  45.         # that you can just ask for its sender, date and so on.
  46.  
  47.         # skip this mail if it's not from the person we're interested
  48.         # in.
  49.         email = message.getaddrlist('From')[0][1]
  50.         if email != victim:
  51.             continue
  52.  
  53.         tt = time.mktime(message.getdate('Date')) % WEEK
  54.         total = total + 1
  55.  
  56.         if (now < tt) and (now + HOUR > tt):
  57.             hit = hit +1
  58.  
  59. # How many messages you can expect to receive from this person in
  60. # the next hour if they were evenly distributed around the week:
  61. baseline = (HOUR * total) / WEEK
  62.  
  63. # How active, relative to baseline, this person really is at this
  64. # hour of the week:
  65. activity = hit / baseline
  66.  
  67. print "%d total messages from %s, %d in this hour of the week." % \
  68.     (total, victim, hit)
  69.  
  70. print "Predicted activity level in the next hour: %f" % (activity)
  71.  
  72. # For bonus points, calculate a 95% confidence interval and integrate
  73. # into your contact manager or mail client.
  74.